/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.wirelesssecuritymodule.services;

import org.testis.data.TestStepDataObject;
import org.testis.data.parser.AtofModuleParameterNames;
import org.testis.nicmodule.core.NetworkInterfaceController;
import org.testis.nicmodule.core.NetworkInterfaceControllerFactory;
import org.testis.services.AtofModuleService;
import org.testis.services.AtofModuleTask;
import org.testis.wirelesssecuritymodule.core.WirelessConnectionTaskExecutor;
import org.testis.wirelesssecuritymodule.data.WirelessSecurityModuleDataObject;

/**
 *
 * @author root
 */
public class WirelessSecurityModuleTask extends AtofModuleTask {

    public WirelessSecurityModuleTask(TestStepDataObject dataObj,
            AtofModuleService service) {
        super(dataObj,
                service);

    }

    @Override
    public void afterExecute() {

        WirelessSecurityModuleDataObject dataObj =
                (WirelessSecurityModuleDataObject) m_service.getDataObj();
        if (dataObj.getConnectionState() != null) {

            NetworkInterfaceController controller;
            String iface = dataObj.getParamInterface();
            try {
                controller =
                        NetworkInterfaceControllerFactory.createInstance("Wlan",
                        dataObj.getParamInterface());
                m_result.getReturnValueList().put(
                        AtofModuleParameterNames.WSM_NIC_OUTPUT_IP,
                        controller.getNetworkOs().getIp(iface));

                m_result.getReturnValueList().put(
                        AtofModuleParameterNames.WSM_NIC_OUTPUT_SUBNET,
                        controller.getNetworkOs().getSubnetMask(iface));

                m_result.getReturnValueList().put(
                        AtofModuleParameterNames.WSM_NIC_OUTPUT_DNS,
                        controller.getNetworkOs().getDns(iface));

                m_result.getReturnValueList().put(
                        AtofModuleParameterNames.WSM_NIC_OUTPUT_GATEWAY,
                        controller.getNetworkOs().getGateway(iface));

                String macAddress = controller.getNetworkOs().getMacAddress(iface);
                String tireli = macAddress.replace(":",
                        "-");
                String noktali = tireli.replace("-",
                        ":");
                m_result.getReturnValueList().put(
                        AtofModuleParameterNames.WSM_NIC_MAC_ADDRESS,
                        tireli);
                m_result.getReturnValueList().put(
                        AtofModuleParameterNames.WSM_NIC_MAC_ADDRESS_DOTTED,
                        noktali);

                m_result.getReturnValueList().put(
                        AtofModuleParameterNames.WSM_NIC_OUTPUT_ENABILITY,
                        ((Boolean) controller.getNetworkOs().isEnabled(iface)).toString());

            } catch (Exception ex) {
            }
            m_result.getReturnValueList().put(
                    AtofModuleParameterNames.WSM_OUTPUT_CONNECTION_STATE,
                    dataObj.getConnectionState());
        }


        super.afterExecute();
        if (m_cancelled) {
            //if wireles step aborted also abort wireless connection thread
            m_service.getDataObj().setStopExecution(true);
            WirelessConnectionTaskExecutor.getInstance().executeTask(
                    m_service.getDataObj());
        }
    }
}
